#include<iostream>
#include<assert.h>
#include<string.h>
#include<string>
#include<algorithm>
using namespace std;
namespace yfy
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{}
		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			reserve(v.capacity());
			for (const auto e : v) {
				push_back(e);
			}
		}
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
			:_start(nullptr)
			, _finish(nullptr)
			, _endofstorage(nullptr)
		{
			while (first != last) {
				push_back(*first);
				first++;
			}
		}
		~vector() {
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}
		iterator begin() {
			return _start;
		}
		iterator end() {
			return _finish;
		}
		const_iterator begin()const {
			return _start;
		}
		const_iterator end()const {
			return _finish;
		}
		size_t capacity() const {
			return _endofstorage - _start;
		}
		size_t size() const {
			return _finish - _start;
		}
		void reserve(size_t num) {
			if (num > capacity()) {
				size_t sz = size();
				T* tmp = new T[num];
				memcpy(tmp, _start, sz * sizeof(T));
				_start = tmp;
				_finish = _start + sz;
				_endofstorage = _start + num;
			}
		}
		iterator insert(iterator pos, const T& num) {
			assert(pos >= begin() && pos <= end());
			if (_finish == _endofstorage) {
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos) {
				*(end + 1) = *end;
				end--;
			}
			*pos = num;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos) {
			assert(pos >= begin() && pos < end());
			//删除指定下标的数据，并把其后的数据依次向前挪动
			iterator it = pos + 1;
			while (it != end()) {
				*(it - 1) = *it;
				it++;
			}
			--_finish;
			return pos;
		}
		void push_back(const T& num) {
			insert(end(), num);
		}
		T& operator[](size_t i) {
			assert(i < size());
			return *(_start + i);
		}
		void swap(vector<T>& v) {
			std::swap(v._start, _start);
			std::swap(v._finish, _finish);
			std::swap(v._endofstorage, _endofstorage);
		}
		vector<T>& operator=(vector<T> v) {
			swap(v);
			return *this;
		}
		void resize(size_t n, const T& val = T()) {
			//开的空间小于size（把超出范围的舍弃）介于size和capacity（初始化_finish以后的空间）
			//大于capacity（要重新开空间，并且初始化_finish以后的空间）
			if (n <= size()) {
				_finish = _start + n;
			}
			else {
				if (n > capacity()) {
					reserve(n);
				}
				while (_finish < _start + n) {
					*_finish = val;
					_finish++;
				}
			}
		}
	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};

	void print(const vector<int>& v) {
		vector<int>::const_iterator it = v.begin();
		while (it != v.end()) {
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}
	void test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);

		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		for (size_t i = 0; i < v.size(); ++i)
		{
			cout << v[i] << " ";
		}
		cout << endl;

		print(v);
	}
	void test_vector2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		print(v);

		v.resize(2);
		print(v);

		v.resize(4);
		print(v);

		v.resize(10, 5);
		print(v);
	}
	void test_vector3()
	{
		vector<string> v;
		string s("hello");
		v.push_back(s);

		v.push_back(string("hello"));
		v.push_back("hello");
		v.push_back("hello");
		v.push_back("hello");
		v.push_back("hello");
		v.push_back("hello");

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_vector4()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);

		vector<int>::iterator pos = find(v.begin(), v.end(), 2);
		if (pos != v.end())
		{
			pos = v.insert(pos, 20);
		}

		cout << *pos << endl;
		*pos = 100;
		++pos;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_vector5()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);

		//要求删除v中所有偶数
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				// erase 返回删除数据的下一个数据的位置
				it = v.erase(it);
			}
			else
			{
				++it;
			}
		}

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_vector8()
	{

		vector<int> v1;
		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<int> v2(v1);		//拷贝构造函数-通过已有对象，复制成为一个新对象
		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;

		string s("abcde");
		vector<int> v3(v1.begin(), v1.end());		//通过（相同或不同）迭代器区间实现拷贝构造
		vector<int> v4(s.begin(), s.end());

		v1 = v4;
		for (auto e : v1)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}



int main()
{
	yfy::test_vector8();
	return 0;
}
